﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Table;

namespace map.manage.Money
{
    public partial class WithDrawDetail : System.Web.UI.Page
    {
        protected cbWithDraw withDraw;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!zfunction.IsAdministrator (AliApi.appUserId ))
            {
                MessageBox.ShowAndRedirect(this.Page,"对不起，您没有权限访问此页面",ResolveUrl ("~/default.aspx"));
                return;
            }
            InitPage();
        }

        #region 辅助方法
        /// <summary>
        /// 返回提现状态的字符串
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        protected string GetWithDrawStatusStr(int status)
        {
            string result = string.Empty;
            if (status == RuleCode.Enum.cbWithDrawStatus.Applicated)
                result = "已申请";
            else if (status == RuleCode.Enum.cbWithDrawStatus.Approved)
                result = "已审核";
            else if (status == RuleCode.Enum.cbWithDrawStatus.Failed)
                result = "提现失败";
            else if (status == RuleCode.Enum.cbWithDrawStatus.Finished)
                result = "提现成功";

            return result;
        }

        private void InitPage()
        {
            ApprovedDiv.Visible = false;
            FinishedDiv.Visible = false;
            NotesDiv.Visible = false;
            
            if (Request["rid"] != null)
            {
                withDraw = cbWithDraw.Find("recid=" + Request["rid"]);
                if (withDraw.Count > 0)
                {
                    ApprovedDiv.Visible = (withDraw.WithDrawStatus.Value == RuleCode.Enum.cbWithDrawStatus.Applicated);
                    FinishedDiv.Visible = (withDraw.WithDrawStatus.Value == RuleCode.Enum.cbWithDrawStatus.Approved);
                    NotesDiv.Visible = (withDraw.WithDrawStatus.Value == RuleCode.Enum.cbWithDrawStatus.Approved );
                }
            }
        }
        #endregion

        #region 操作
        protected void ApprovedButton_Click(object sender, EventArgs e)
        {
            if (withDraw.Count >0 && AliApi.appUserId !=string.Empty )
            {
                withDraw.WithDrawStatus.Value = RuleCode.Enum.cbWithDrawStatus.Approved;
                withDraw.ApprovedDate.Value = DateTime.Now;
                withDraw.ApprovedBy.Value = AliApi.appUserId;
                withDraw.Modify();

                InitPage();
            }
        }

        protected void FailedButton_Click(object sender, EventArgs e)
        {
            if (withDraw.Count > 0 && AliApi.appUserId != string.Empty)
            {
                withDraw.WithDrawStatus.Value = RuleCode.Enum.cbWithDrawStatus.Failed ;
                withDraw.FinishedDate .Value = DateTime.Now;
                withDraw.FinishedBy .Value = AliApi.appUserId;
                withDraw.Notes.Value = WithDrawNoteTextArea.Value;
                withDraw.Modify();

                InitPage();
            }
        }

        protected void FinishedButton_Click(object sender, EventArgs e)
        {
            if (withDraw.Count > 0 && AliApi.appUserId != string.Empty)
            {
                withDraw.WithDrawStatus.Value = RuleCode.Enum.cbWithDrawStatus.Finished ;
                withDraw.FinishedDate.Value = DateTime.Now;
                withDraw.FinishedBy.Value = AliApi.appUserId;
                withDraw.Notes.Value = WithDrawNoteTextArea.Value;
                withDraw.Modify();

                InitPage();
            }
        }
        #endregion
    }
}
